<?php

	class AKB_GLOSSARY_PANEL extends AKB_PANEL
	{
		var $words = array();

		/**
		* SetPanelSettings
		* This function is a chance to set any last minute global variables for
		* the template to use
		*
		* @return void
		*/
		function SetPanelSettings()
		{
			//Derive from the URL
			$full_url = $_SERVER['REQUEST_URI'];
			$letter = strtoupper(substr($full_url,strrpos($full_url,"/")+1));

			$letter = urldecode($letter);

			if ($letter == 'GLOSSARY.PHP') {
				$letter = '';
			}
			//Remain backwards compat in case any old links out there are stored the old way.

			if ((strlen($letter) > 1) && (isset($_GET['letter']))) {
				$letter = isset($_GET['letter']) ? $_GET['letter'] : '';
			}

			$wordid = isset($_GET['wordid']) ? strtolower($_GET['wordid']) : '';

			$GLOBALS['GlossaryGrid'] = '';
			$GLOBALS['ShowHeadings'] = 'table';
			$GLOBALS['ShowMessage'] = 'none';

			if (empty($this->words)) {
				$this->GetList($letter);
			}

			if (!empty($this->words)) {
				foreach ($this->words as $word => $row) {
					$GLOBALS['Word'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders(htmlspecialchars($word, ENT_QUOTES, $GLOBALS['charset']));
					$GLOBALS['Description'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($row['description']);
					$GLOBALS['GlossaryId'] = $row['glossaryid'];
					if ($row['glossaryid'] == $wordid) {
						$GLOBALS['GlossaryGrid'] .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('GlossaryRowSelected');
					} else {
						$GLOBALS['GlossaryGrid'] .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('GlossaryRow');
					}
				}
			} else if (!empty($letter)) {
				$GLOBALS['ShowHeadings'] = 'none';
				$GLOBALS['ShowMessage'] = 'block';
				$GLOBALS['Message'] = sprintf(GetLang('NoGlossaryWordsStartingWith'), htmlspecialchars($letter, ENT_QUOTES, $GLOBALS['charset']));
			} else {
				$GLOBALS['ShowHeadings'] = 'none';
				$GLOBALS['ShowMessage'] = 'block';
				$GLOBALS['Message'] = GetLang('NoGlossaryWords');
			}

			$GLOBALS['AlphaList'] = '';

			$GLOBALS['Href'] = $GLOBALS['pathToKB'].'/glossary.php';
			$GLOBALS['Text'] = '%%LNG_All%%';
			$GLOBALS['Class'] = (!isset($_GET['letter'])) ? 'current' : '';
			$GLOBALS['AlphaList'] .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('GlossaryLetterLink');
			$letters = GetAlphaList();
			foreach ($letters as $letter) {
				if ($GLOBALS['cleanUrls']) {
					$GLOBALS['Href'] = $GLOBALS['pathToKB'].'/glossary.php/letter/'.$letter;
				} else {
					$GLOBALS['Href'] = $GLOBALS['pathToKB'].'/glossary.php?letter='.$letter;
				}

				$GLOBALS['Text'] = $letter;
				$GLOBALS['Class'] = (isset($_GET['letter']) && $letter == $_GET['letter']) ? 'current' : '';
				$GLOBALS['AlphaList'] .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('GlossaryLetterLink');
			}
		}

		/**
		* GetList
		* Get the list of glossary terms from the database and save them in an array
		*
		* @param string $letter Get the list with words starting with $letter
		*
		* @return void
		*/
		function GetList($letter)
		{
			$query = 'SELECT glossaryid, word, description
				FROM '.$GLOBALS['tablePrefix'].'glossary ';
			$letters = GetAlphaList();

			if (in_array($letter, $letters)) {

				$query .= " WHERE word LIKE '".urldecode($letter)."%'";
			}
			$query .= ' ORDER BY word ASC';

			$result = $GLOBALS['AKB_DB']->Query($query);
			while ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
				$this->words[$row['word']] = $row;
			}
		}

	}
?>
